﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Microsoft.VisualBasic;

namespace TIMSComponents
{
    internal static  class CMain
    {
  
        
        private static  string[] SplitString(string str, string[] spliter)
        {
            if (str != null)
            {
                string[] StrA = str.Split(spliter, StringSplitOptions.RemoveEmptyEntries);
                return StrA;
            }
            else
            {
                return null;
            }
        }

        private static string ArrayItemsToStr(string[] MyArray)
        {
            if (MyArray != null)
            {
                string Result = "";
                foreach (string str in MyArray)
                {
                    Result = Result + "," + str;
                }
                //Cut first symbol ","
                return Strings.Mid(Result, 2, Result.Length);
            }
            else
            {
                return null;
            }
        }

        internal static MyFindType FindType(String StrForGues)
        {
            if (StrForGues != null)
            {
                //if first character of string is % or *
                if (Strings.Mid(StrForGues, 1, 1) == "%" || Strings.Mid(StrForGues, 1, 1) == "*")
                {
                    return MyFindType.InText;
                }
                //if string found
                else if (Strings.InStr(StrForGues, "..", CompareMethod.Text) > 0)
                {
                    return MyFindType.Both;
                }
                else
                {
                    return MyFindType.FromFirstChars;
                }
            }
            else
            {
                return MyFindType.FromFirstChars;
            }
        }


        internal static string GetSelectCommand(string SqlCommandText, MyCommandType CommType, string[] FieldList, string SqlParams)
        {
            string FieldList1 = ArrayItemsToStr(FieldList);
            if (FieldList!= null && string.IsNullOrEmpty(SqlCommandText) ==false) //&& )
            {
                if (CommType == MyCommandType.SqlFunction)
                {
                    return "Select " + FieldList1 + " from " + SqlCommandText + "(" + SqlParams + ")";
                }
                else //if (CommType == MyCommandType.SqlStoredProcedure)
                {
                    return null;
                }                
            }
            else
            {
                return null;
            }
        }
        
        //Get where condition for filtering data
        internal static string GetWhereCondition(string FieldKeyNN, string FieldKeyNS, string EnteredText)
        {
            string FieldKey;
            string WhereCondition;
            if (Microsoft.VisualBasic.Information.IsNumeric(EnteredText))
            {
                FieldKey = FieldKeyNN;
            }
            else
            {
                FieldKey = FieldKeyNS;
            }

            if (FieldKey != null && EnteredText!=null)
            {
                if (FindType(EnteredText) == MyFindType.Both)
                {                
                    string[] splitedS = SplitString(EnteredText, new string[] { ".." });
                    string StrForReplace = "%') And " + FieldKey + " Like ('%";
                    WhereCondition= FieldKey +" like ('%" + Strings.Replace(ArrayItemsToStr(splitedS), "," , StrForReplace,1,-1,CompareMethod.Text)+ "%')";  
                }
                else if (FindType(EnteredText) == MyFindType.InText)
                {
                    Strings.Replace(EnteredText, "*", "", 1, -1, CompareMethod.Text);
                    Strings.Replace(EnteredText, "%", "", 1, -1, CompareMethod.Text);
                    WhereCondition = FieldKey + " Like ('%" + EnteredText + "%')";                
                }
                else
                {
                    WhereCondition = FieldKey + " Like ('" + EnteredText + "%')";
                }
                return WhereCondition;
            }
            else
            {
                return null;
            }
        }

        internal static DataTable BuildDataTableStructure(string[] fieldList)
        {
            DataTable dt=new DataTable();
            if (fieldList != null)
            {
                foreach (string Item in fieldList)
                {
                    dt.Columns.Add(Item, System.Type.GetType("System.String"));                    
                }
                return dt.Clone();
            }
            else
            {
                return null; 
            }
            
        }

    }
}

